﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 自旋锁
{
    class Program
    {
        class A
        {
            public int num = 0;
            public A(int i) { num = i; }

        }

        static QueueLock<A> msgQueue1 = new QueueLock<A>();
        static bool endflag1 = false;

        static QueueSpinLock<A> msgQueue2 = new QueueSpinLock<A>();
        static bool endflag2 = false;

        static void Main(string[] args)
        {
            Console.WriteLine("程序启动 " + DateTime.Now);
            Thread[] tharr1 = new Thread[4];
            for (int i = 0; i < tharr1.Length; i++)
            {
                tharr1[i] = new Thread(ThreadFun1);
                tharr1[i].IsBackground = true;
                tharr1[i].Start(i.ToString());
            }
            while (endflag1 != true) Thread.Sleep(100);

            Thread[] tharr2 = new Thread[4];
            for (int i = 0; i < tharr2.Length; i++)
            {
                tharr2[i] = new Thread(ThreadFun2);
                tharr2[i].IsBackground = true;
                tharr2[i].Start(i.ToString());
            }
            while (endflag2 != true) Thread.Sleep(100);

            Console.ReadKey();
        }


        static private void ThreadFun1(object num)
        {
            int flag = Convert.ToInt32(num);

            if (flag == 0)
            {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue1.InQueue(new A(i + 1));
                }

                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else if (flag == 1)
            {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue1.InQueue(new A(i + 1));
                }

                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else if (flag == 2)
            {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue1.InQueue(new A(i + 1));
                }

                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else
            {
                int count = 0;
                int queueCnt = 0;
                do
                {
                    Thread.Sleep(10);
                    queueCnt = msgQueue1.GetQueueCount();
                    for (int i = 0; i < queueCnt; i++)
                    {
                        A a = msgQueue1.OutQueue();
                        //Console.WriteLine(a.num);
                        count++;
                    }
                    Thread.Sleep(5);
                    queueCnt = 0;
                    queueCnt = msgQueue1.GetQueueCount();
                } while (queueCnt > 0);
                endflag1 = true;
                Console.WriteLine("共取出数据：" + count.ToString() + " " + DateTime.Now);
            }

        }


        static private void ThreadFun2(object num)
        {
            int flag = Convert.ToInt32(num);

            if (flag == 0)
            {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue2.InQueue(new A(i + 1));
                }

                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else if (flag == 1)
            {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue2.InQueue(new A(i + 1));
                }

                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else if (flag == 2)
            {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue2.InQueue(new A(i + 1));
                }

                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else
            {
                int count = 0;
                int queueCnt = 0;
                do
                {
                    Thread.Sleep(10);
                    queueCnt = msgQueue2.GetQueueCount();
                    for (int i = 0; i < queueCnt; i++)
                    {
                        A a = msgQueue2.OutQueue();
                        //Console.WriteLine(a.num);
                        count++;
                    }
                    Thread.Sleep(5);
                    queueCnt = 0;
                    queueCnt = msgQueue2.GetQueueCount();
                } while (queueCnt > 0);
                endflag2 = true;
                Console.WriteLine("共取出数据：" + count.ToString() + " " + DateTime.Now);
            }

        }

    }
}
